package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.YueFanAdapter;
import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.YueFanHuoDongTask;
import com.xgkj.diyiketang.util.EatShowSetting;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/4/13.
 */
public class YueFanActivity extends BaseActivity implements View.OnClickListener, PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener{
    private Context mContext;

    private PullToRefreshListView bdListView;
    private PullToRefreshListView qgListView;
    private int currentPage_bd = 1;
    private int currentPage_qg = 1;
    private int size = 10;
    private YueFanAdapter bdAdapter;
    private YueFanAdapter qgAdapter;
    private int bdTaskId = -1;
    private int qgTaskId = -1;
    private CheckBox mBendi;
    private CheckBox mQuanguo;
    private TextView mWangqi;
    private TextView back;
    private String mLocalString;
    private ImageView sousuo;
    private LinearLayout mEmptyView;

    private LinearLayout mEmptyContentView;
    private RelativeLayout noneLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuefan);
        mContext = this;
        initView();
    }

    private void initView(){
        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        noneLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mLocalString = EatShowSetting.getInstance(mContext).getLocal();
        sousuo = (ImageView) findViewById(R.id.yuefan_search);
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, SearchYueFanActivity.class));
            }
        });
        mBendi = (CheckBox) findViewById(R.id.bendi_btn);
        mBendi.setOnClickListener(this);
        mBendi.setChecked(true);

        mQuanguo = (CheckBox) findViewById(R.id.quangguo_btn);
        mQuanguo.setOnClickListener(this);
        mQuanguo.setChecked(false);

        bdListView = (PullToRefreshListView)findViewById(R.id.yf_bendi_list);
        qgListView = (PullToRefreshListView)findViewById(R.id.yf_quanguo_list);
        bdListView.setVisibility(View.VISIBLE);
        qgListView.setVisibility(View.GONE);

        bdListView.init(mContext, true);
        bdAdapter = new YueFanAdapter(mContext);
        bdListView.setAdapter(bdAdapter, true);
        bdListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(bdListView.getHeaderViewsCount()>0){
                    position--;
                }
                if (position<0 || position>=bdAdapter.getCount())return;
                HuoDongInfo fdinfo = (HuoDongInfo) bdAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                Intent bdInent = new Intent(mContext, YuFanDetailActivity.class);
                bdInent.putExtra("ACTIVEID",activeid);
                bdInent.putExtra("LOGO",icon);
                startActivity(bdInent);
            }
        });
        bdListView.setOnRefreshListener(this);
        bdListView.setOnLoadDataListener(this);
        bdListView.doRefresh();


        qgListView.init(mContext, true);
        qgAdapter = new YueFanAdapter(mContext);
        qgListView.setAdapter(qgAdapter, true);
        qgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(qgListView.getHeaderViewsCount()>0){
                    position--;
                }
                if (position<0||position>=qgAdapter.getCount())return;
                HuoDongInfo fdinfo = (HuoDongInfo) qgAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                Intent bdInent = new Intent(mContext, YuFanDetailActivity.class);
                bdInent.putExtra("ACTIVEID",activeid);
                bdInent.putExtra("LOGO",icon);
                startActivity(bdInent);
            }
        });
        qgListView.setOnRefreshListener(this);
        qgListView.setOnLoadDataListener(this);
        qgListView.doRefresh();

        mWangqi = (TextView) findViewById(R.id.wangqi);
        mWangqi.setOnClickListener(this);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getBenDiList();

    }


    private void getBenDiList() {

        if (bdTaskId != -1) {
            return;
        }

        if (currentPage_bd == 0) {
            bdListView.setNoNextPage();
            return;
        }

        YueFanHuoDongTask task = new YueFanHuoDongTask(size, currentPage_bd,mLocalString,"");
        task.addListener(yuefanListenter);
        bdTaskId = getEatShowApp().getTaskManager().addTask(task);

    }

    private void getQuanGuoList() {

        if (qgTaskId != -1) {
            return;
        }

        if (currentPage_qg == 0) {
            qgListView.setNoNextPage();
            return;
        }

        YueFanHuoDongTask task = new YueFanHuoDongTask(size, currentPage_bd,"","");
        task.addListener(yuefanListenter);
        qgTaskId = getEatShowApp().getTaskManager().addTask(task);

    }


    private TaskEventListener yuefanListenter = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == bdTaskId) {
                bdTaskId = -1;

                if (currentPage_bd == 1) {
                    bdAdapter.clearData();
                    bdListView.onRefreshComplete();
                    bdListView.enableLoadMore();
                } else {
                    bdListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if (data != null) {
                    fansList = (List<HuoDongInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    bdAdapter.updateData(fansList);
                }
                // mAreaId="";
                currentPage_bd = result;
                if(bdAdapter != null && bdAdapter.getCount() == 0){
                    noneLayout.setVisibility(View.VISIBLE);
                    EmptyDataUtil.updateWenziEmptyContentView(mEmptyView, mEmptyContentView, bdListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentView(mEmptyView, mEmptyContentView, bdListView);
                }


            } else if (taskId == qgTaskId) {
                qgTaskId = -1;
                // sAdapter.clearData();
                if (currentPage_qg == 1) {
                    qgAdapter.clearData();
                    qgListView.onRefreshComplete();
                    qgListView.enableLoadMore();
                } else {
                    qgListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if (data != null) {
                    fansList = (List<HuoDongInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    qgAdapter.updateData(fansList);
                }
                //mAreaId="";
                currentPage_qg = result;
                if(qgAdapter != null && qgAdapter.getCount() == 0){
                    noneLayout.setVisibility(View.VISIBLE);
                    EmptyDataUtil.updateWenziEmptyContentView(mEmptyView, mEmptyContentView, qgListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentView(mEmptyView, mEmptyContentView, qgListView);
                }


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == bdTaskId) {
                bdTaskId = -1;
                if (currentPage_bd == 1) {
                    bdListView.onRefreshComplete();
                } else {
                    bdListView.onLoadDataComplete(false);
                }
            } else if (taskId == qgTaskId) {
                qgTaskId = -1;
                if (currentPage_qg == 1) {
                    qgListView.onRefreshComplete();
                } else {
                    qgListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bendi_btn:
                mBendi.setChecked(true);
                mQuanguo.setChecked(false);
                qgListView.setVisibility(View.GONE);
                bdListView.setVisibility(View.VISIBLE);
                noneLayout.setVisibility(View.GONE);
                if (bdAdapter != null && bdAdapter.getCount() == 0) {
                    bdAdapter.clearData();
                    currentPage_bd =1;
                    getBenDiList();
                }
                break;
            case R.id.quangguo_btn:
                mQuanguo.setChecked(true);
                mBendi.setChecked(false);
                qgListView.setVisibility(View.VISIBLE);
                bdListView.setVisibility(View.GONE);
                noneLayout.setVisibility(View.GONE);
                if (qgAdapter != null && qgAdapter.getCount() == 0) {
                    qgAdapter.clearData();
                    currentPage_qg = 1;
                    getQuanGuoList();
                }
                break;
            case R.id.wangqi:
                Intent wIntent = new Intent(mContext,WangQiHuoDongActivity.class);
                startActivity(wIntent);
                break;
        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        if (bdListView.getVisibility() == View.VISIBLE) {
            getBenDiList();

        } else if (qgListView.getVisibility() == View.VISIBLE) {
           getQuanGuoList();

        }
    }

    @Override
    public void onRefresh() {
        if (bdListView.getVisibility() == View.VISIBLE) {
            currentPage_bd = 1;
            getBenDiList();

        } else if (qgListView.getVisibility() == View.VISIBLE) {
            currentPage_qg = 1;
            getQuanGuoList();

        }
    }
}
